﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using System.Web.Compilation;

namespace TestProject
{
	public delegate void StringProcess(string input);

	public class Person
	{
		private string name;
		public Person(string name)
		{
			this.name = name;
		}
		public void Say(string message)
		{
			Console.WriteLine("{0} say:{1}", name, message);
		}
	}

	public class Background
	{
		public static void Note(string note)
		{
			Console.WriteLine(note);
			//获取两个整数，返回一个字符，委托
			Func<int, int, string> fun = (x, y) => (x * y).ToString();
			Console.WriteLine(fun(3, 4));
		}
	}

	public class SimpleDelegate
	{
		static void main()
		{
			Person john = new Person("john");
			Person tom = new Person("tom");
			StringProcess johnsvoice, tomsvoice, backgroud;
			johnsvoice = new StringProcess(john.Say);
			tomsvoice = new StringProcess(tom.Say);
			backgroud = new StringProcess(Background.Note);
			johnsvoice("hello,tom.");
			tomsvoice.Invoke("tom,ok.");
			backgroud("New file");
		}
	}
	public static class Chapter1
	{

	}



	public class DictionaryTest
	{
		public static Dictionary<string, int> GetWord(string text)
		{
			Dictionary<string, int> frequent = new Dictionary<string, int>();
			string[] words = Regex.Split(text, @"\W+");
			foreach (var word in words)
			{
				if (frequent.ContainsKey(word))
					frequent[word]++;
				else
					frequent[word] = 1;
			}
			return frequent;
		}

		public static double TakeSquareRoot(int x)
		{
			return Math.Sqrt(x);
		}

		public static void IntToDoubleList()
		{
			List<int> inter = new List<int>();
			inter.Add(1);
			inter.Add(2);
			inter.Add(3);
			inter.Add(4);
			inter.Add(5);
			Converter<int, double> convert = TakeSquareRoot;
			List<double> doubles = inter.ConvertAll<double>(convert);

			foreach (var d in doubles)
			{
				Console.WriteLine(d);
			}
		}

		public static List<T> MakeList<T>(T first, T second)
		{
			List<T> list = new List<T>();
			list.Add(first);
			list.Add(second);
			return list;
		}

		public static int CompareToDefault<T>(T value)
			where T : IComparable<T>
		{
			return value.CompareTo(default(T));
		}

		public static void CompareTest()
		{
			Console.WriteLine(CompareToDefault("x"));
			Console.WriteLine(CompareToDefault(10));
			Console.WriteLine(CompareToDefault(0));
			Console.WriteLine(CompareToDefault(-1));
			Console.WriteLine(CompareToDefault(DateTime.MinValue));
			Console.WriteLine(DateTime.MinValue);
		}
	}
	public class Sample<T>
		where T : class, IEnumerable<string>, IEqualityComparer<T>, IEquatable<T>
	{
		public static bool Equal(T t)
		{
			return t.Equals(default(T));
		}
	}
	
	public sealed class Pair<T1, T2> : IEquatable<Pair<T1, T2>>
	{
		private static readonly IEqualityComparer<T1> FirstComparer =
			EqualityComparer<T1>.Default;
		private static readonly IEqualityComparer<T2> SecondComparer
			= EqualityComparer<T2>.Default;
		private readonly T1 first;
		private readonly T2 second;
		public Pair(T1 first, T2 second)
		{
			this.first = first;
			this.second = second;
		}
		public T1 First
		{
			get
			{
				return first;
			}
		}
		public T2 Second
		{
			get
			{
				return second;
			}
		}
		public bool Equals(Pair<T1, T2> other)
		{
			return other != null &&
				FirstComparer.Equals(this.First, other.First) &&
				SecondComparer.Equals(this.second, other.Second);
		}
		public override bool Equals(object obj)
		{
			return Equals(obj as Pair<T1, T2>);
		}
		public override int GetHashCode()
		{
			return FirstComparer.GetHashCode(first) * 37 + SecondComparer.GetHashCode(second);
		}
	}
	public class Outer
	{
		public class Inner<U, V>
		{
			static Inner()
			{
				Console.WriteLine("Outer<{0}>.Inner<{1},{2}>",
					typeof(U).Name,
					typeof(V).Name);
			}
			public static void DummyMethod()
			{
			}
		}
		public static void TypeofDemo<X>()
		{
			Console.WriteLine(typeof(X));
			Console.WriteLine(typeof(List<>));
			Console.WriteLine(typeof(Dictionary<,>));
			Console.WriteLine(typeof(List<X>));
			Console.WriteLine(typeof(Dictionary<string, X>));
			Console.WriteLine(typeof(List<long>));
			Console.WriteLine(typeof(Dictionary<long, Guid>));
		}
		public static void Reflection()
		{
			var ass = AppDomain.CurrentDomain.GetAssemblies();
			string assmem = string.Join(System.Environment.NewLine, ass.Select(x => x.FullName));
			Console.WriteLine(assmem);
			//string listTypeName = "System.Collections.Generic.List~1";
			//Type defName = Type.GetType(listTypeName);
			//Type closedByName = Type.GetType(listTypeName+"[System.String]");
			//Type closedByMethod = defName.MakeGenericType(typeof(string));
			//Type closedByTypeof= typeof(string);
			//Console.WriteLine(closedByName == closedByMethod);
			//Console.WriteLine(closedByName == closedByTypeof);
		}

		public static void AppDomainDemo()
		{
			string[] file = Directory.GetFiles(@"E:\MyWork\TcOpenAPI\Lib\SOAEntity", "*.dll", SearchOption.TopDirectoryOnly);
			AppDomainSetup setup = new AppDomainSetup();
			setup.LoaderOptimization = LoaderOptimization.SingleDomain;
			setup.ApplicationBase = "C:\\Test";
			AppDomain domain = AppDomain.CreateDomain("NewDomain", null, setup);
			//domain.ExecuteAssembly("");
			ReadOnlyCollection<Assembly> AllAssemlies =
				new ReadOnlyCollection<Assembly>(BuildManager.GetReferencedAssemblies().Cast<Assembly>().ToList());
		}
	}
}
